package com.jy.api.service.impl;

import com.jy.api.dao.StockProductDao;
import com.jy.api.dao.StockSpecDao;
import com.jy.api.domain.StockProduct;
import com.jy.api.domain.StockProductType;
import com.jy.api.domain.StockSpec;
import com.jy.api.service.StockSpecService;
import com.jy.api.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xiaohua on 2019/4/19.
 */
@Service
public class StockSpecServiceImpl implements StockSpecService {
   @Autowired
   private StockSpecDao stockSpecDao;
   @Autowired
   private StockProductDao productDao;

    @Override
    public List<StockSpec> findAll() {
       return stockSpecDao.findAllByEnabled(true);
    }

    @Override
    public void updateOrSave(String name, int id) {
        StockSpec type;
        if (id==0){
            type = new StockSpec();
            type.setName(name);
        }else {
            type =stockSpecDao.findOne(id);
            type.setName(name);
        }
        stockSpecDao.save(type);
    }

    @Override
    public Result deleteById(int id) {
        List<StockProduct> bySpecIdAndEnabled = productDao.findBySpecIdAndEnabled(id, true);
        if (bySpecIdAndEnabled.isEmpty()){
            StockSpec type= stockSpecDao.findOne(id);
            type.setEnabled(false);
            stockSpecDao.save(type);
            return new Result(200,"规格移除成功");
        }else {
            return new Result(400,"该规格下还有商品，请核实后进行移除");
        }
    }
}
